package com.chao.product.conf.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *  主题模式   消息被发到匹配的RoutingKey的Queue中
 *  “#” : 匹配一个或者多个词  RoutingKey 用“.”做分隔符
 * “*”：匹配一个词
 */
@Configuration
public class TopicConfig {

    @Bean
    public Queue topicQueue1(){
        Queue queue = new Queue("topic_queue_1");
        queue.addArgument("x-dead-letter-exchange",setDeadTopicExchange().getName());//设置死信交换机
        queue.addArgument("x-dead-letter-routing-key",setBindTopicQueue3().getRoutingKey());//设置死信队列
        queue.addArgument("x-message-ttl",1000);//设置延迟时间
        return queue;
    }

    @Bean
    public Queue topicQueue2(){
        return new Queue("topic_queue_2");
    }


    @Bean
    public TopicExchange setTopicExchange(){
       return ExchangeBuilder.topicExchange("topic_exchange").build();
    }

    @Bean
    public Binding setBindTopicQueue1(){
        return BindingBuilder.bind(topicQueue1()).to(setTopicExchange()).with("topic.queue.1.*");
    }

    @Bean
    public Binding setBindTopicQueue2(){
        return BindingBuilder.bind(topicQueue2()).to(setTopicExchange()).with("topic.queue.2.#");
    }


    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue deadQueue1(){
        Queue queue = new Queue("dead_queue_1");
        return queue;
    }

    /**
     * 创建死信交换机
     * @return
     */
    @Bean
    public TopicExchange setDeadTopicExchange(){
        TopicExchange topicExchange = ExchangeBuilder.topicExchange("dead_topic_exchange").build();
        return topicExchange;
    }

    /**
     * 死信队列 绑定死信交换机
     * @return
     */
    @Bean
    public Binding setBindTopicQueue3(){
        return BindingBuilder.bind(deadQueue1()).to(setDeadTopicExchange()).with("dead.queue.1.#");
    }
}
